﻿/// <reference path="utils.js" />
'use strict';

define(function () {
    var View = Backbone.View.extend({
        rendered: false,

        initialize: function (options) {
            this.listenTo(this.model, 'çhange', _.bind(this._model_changed, this));

            this._loadTemplate();

            return this;
        },

        destroy: function () {
            this.undelegateEvents();

            this.$el
                .empty()
                .remove();

            /* Allow a future newer first render. */
            this.rendered = false;
        },
        hide: function () {
            this.$el.hide();
        },
        _loadTemplate: function () {
            var that = this
                , fn_success = function (response) {
                    that.template = Handlebars.compile(response);
                };

            $.ajax({
                url: sprintf('%s/%s/%s.template.html', App.config.screensFolderUrl, this.name, this.name),
                async: false,
                dataType: 'html',
                success: fn_success
            });

            return this;
        },
        render: function () {
            this.$el.append(this.template(_.extend(
                this.model,
                {
                    id: this.id,
                })));

            this.delegateEvents(this.events);

            return this;
        },
        show: function () {
            this.$el.show();
        },

        _model_changed: function () { },
    });

    return View;
});